package com.example.demo.security.authentication;

import lombok.Data;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;

import java.util.Collection;

/**
 * describe: 自定义的认证主体
 *
 * @author lianying
 * @date 2020/12/06
 */
@Data
public class CustomUsernamePasswordAuthentication implements Authentication {
    private String username;
    private String password;
    private String code;
    private Collection<? extends GrantedAuthority> roles;
    private UserInfo userInfo;
    private boolean isAuthenticated =false;

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return roles;
    }

    @Override
    public Object getCredentials() {
        return this.password;
    }

    @Override
    public Object getDetails() {
        return userInfo;
    }

    @Override
    public Object getPrincipal() {
        return this.username;
    }

    @Override
    public boolean isAuthenticated() {
        return this.isAuthenticated;
    }

    @Override
    public void setAuthenticated(boolean isAuthenticated) throws IllegalArgumentException {
        this.isAuthenticated=isAuthenticated;
    }

    @Override
    public String getName() {
        return "自定义用户名密码认证主体";
    }

    @Data
    public static class UserInfo{
        private String name;
        private Integer age;
    }
}
